package org.particlethink.auth;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class TokenFilter extends OncePerRequestFilter {

    private final static Logger logger = LoggerFactory.getLogger(TokenFilter.class);

    private UserTokenWithRedisHandlerAdapter handlerAdapter;

    public void setHandlerAdapter(UserTokenWithRedisHandlerAdapter handlerAdapter) {
        this.handlerAdapter = handlerAdapter;
    }

//    @Override
//    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//
//    }

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        try {
            if (handlerAdapter.handleRequest(request)) {
                logger.info("Request URL: {}", request.getRequestURL());
                logger.info("Request Method: {}", request.getMethod());
                logger.info("Request Parameters: {}", request.getParameterMap());
                logger.info("authentication success");
            } else {
                logger.info("authentication fail");
            }
        } finally {
            filterChain.doFilter(request, response);
        }
    }
}
